﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using smartphone.App_Code;
using System.Data;
using System.Text;

namespace smartphone.UserControls
{
    public partial class AdminAddProduct : System.Web.UI.UserControl
    {
        private Product product = new Product();
        private string[] image;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateControl();
            }
        }

        /// <summary>
        /// Hien thi danh sach chon cac hang san xuat
        /// </summary>
        private void PopulateControl()
        {
            //Lay danh sach hang san xuat
            Manufacturer manu = new Manufacturer();
            DropDownListManu.DataSource = manu.getManufacturer();
            DropDownListManu.DataTextField = "tenHang";
            DropDownListManu.DataValueField = "maHang";
            //DropDownListManu.SelectedValue = product.ManufacturerName;
            DropDownListManu.DataBind();
        }

        /// <summary>
        /// Hien thi messagebox thong bao loi
        /// </summary>
        /// <param name="msg">Nội dung thông báo được đưa ra</param>
        private void ShowPopUpMsg(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
        }

        protected void BtAddProduct_Click(object sender, EventArgs e)
        {
            string name = TxtTitle.Text;
            int manu = Int32.Parse(DropDownListManu.SelectedValue);
            string content = EditorDescription.Content;
            int quantity = Int32.Parse(TxtQuantity.Text);
            double price = double.Parse(TxtPrice.Text);
            string date = String.Format(TxtImportDate.Text, "yyyy-MM-dd");
            DateTime importDate = DateTime.Parse(date);
            string accessory = TxtPK.Text;
            int warranty = Int32.Parse(TxtWaranty.Text);
            bool record = CkRecord.Checked;
            string os = TxtOS.Text;
            string cpu = TxtCpu.Text;
            float camera = float.Parse(TxtCamera.Text);
            string wifi = TxtWifi.Text;
            string music = TxtMusic.Text;
            string sensor = TxtSensor.Text;
            string movie = TxtMovie.Text;
            bool publish = CheckBoxPublish.Checked;

            //hinh dai dien cua san pham
            string newImgName = FileUploadImage.FileName;
            string location = Server.MapPath("./ProductImages/") + newImgName;
            FileUploadImage.SaveAs(location);

            try
            {
                bool success = product.CreateProduct(name, manu, newImgName, content, quantity, price,
                    importDate, accessory, warranty, record, os, cpu, camera, wifi,
                    music, sensor, movie, publish);
            }
            catch
            {

            }
        }

        //private int GetProductID()
        //{

        //}

        //protected void AjaxFileUploadAlbum_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        //{
        //    string name = e.FileName;
        //    string location = Server.MapPath("./ProductImages/") + name;
        //    try
        //    {
        //        //product.CreateProductAlbum(
        //    }
        //    catch
        //    {

        //    }
        //    AjaxFileUploadAlbum.SaveAs(location);
        //}

    }
}